#include "createActorButton.h"
#include "renderer.h"
#include "input.h"


CreateActorButton::CreateActorButton(){
newActor=NULL;
bDragable=true;
name="createActorButton";
}

CreateActorButton::~CreateActorButton(){
}

void CreateActorButton::mouseDrag(){

    input->dragButton=this;
    setLocation(Vector3f(input->mouseX-scale.x/2,input->mouseY-scale.y/2,0));

}

void CreateActorButton::finishDrag(){

    renderer->pick(input->mouseX, input->mouseY);

    cout << "Picking coordinates: " << input->mouse3D << endl;

    uint myActorSize=renderer->actorList.size();
    uint myButtonSize=renderer->buttonList.size();

    renderer->actorInfo[buttonProperty].actorReference->create();
    if (myActorSize<renderer->actorList.size()){
        newActor=renderer->actorList.back();
        newActor->bPickable=false;
        }

    else if (myButtonSize<renderer->buttonList.size()){
        cout << "creating a new Button too!" << endl;
        newActor=renderer->buttonList.back();
        newActor->setup();
        newActor->location=Vector3f(input->mouseX,input->mouseY,0.0);
        setLocation(initialLocation);
        newActor=NULL;
        return;
        }

    //draw as new actor as VBOMESH if we have one assigned to us and we find it in the vboMeshList
    if (vboMeshID!="NULL" && renderer->vboList[vboMeshID]){
        newActor->vboMeshID=vboMeshID;
        if (renderer->vboList[vboMeshID]->bIsSkeletal){          //see if we're skeletal and stuff...
            newActor->drawType=DRAW_SKELETAL;
            newActor->sceneShaderID="skeletal";
            }
        else{
            newActor->sceneShaderID="color";
            newActor->drawType=DRAW_VBOMESH;
            }
    }
    cout << "created new actor..." << endl;

    //also create a node for our actor!
    if (input->bShiftDown){

        renderer->actorInfo["8RootNode"].actorReference->create();
        newActor->baseNode=renderer->nodeList.back();

        newActor->baseNode->location.x=100;
        newActor->baseNode->location.y=100;

        newActor->baseNode->setup();

        cout << "created root node: " << newActor->baseNode->name << endl;
        }

    std::cout << "created new Actor of type " << newActor->name << endl;

    setLocation(initialLocation);
    newActor->setLocation(input->mouse3D);
    newActor->bPickable=true;

    newActor->setup();

    cout << "set up new Actor..." << endl;

    newActor=NULL;
}

void CreateActorButton::clickedLeft(){

    input->focusButton=this;

    uint myActorSize=renderer->actorList.size();
    uint myButtonSize=renderer->buttonList.size();

    renderer->actorInfo[buttonProperty].actorReference->create();
    if (myActorSize<renderer->actorList.size()){
        newActor=renderer->actorList.back();
        }

    else if (myButtonSize<renderer->buttonList.size()){
        newActor=renderer->buttonList.back();
        newActor->setup();
        //newActor->location=Vector3f(input->mouseX,input->mouseY,0.0);
        setLocation(initialLocation);
        //newActor=NULL;
        return;
        }


    if (vboMeshID!="NULL" && renderer->vboList[vboMeshID]){
        newActor->vboMeshID=vboMeshID;
        if (renderer->vboList[vboMeshID]->bIsSkeletal){          //see if we're skeletal and stuff...
            newActor->drawType=DRAW_SKELETAL;
            newActor->sceneShaderID="skeletal";
            }
        else{
            newActor->drawType=DRAW_VBOMESH;
            }
    }
    cout << "created new actor..." << endl;

    newActor->setup();

    cout << "set up new Actor..." << endl;

    newActor->bPickable=false;
    std::cout << "created new Actor of type " << newActor->name << endl;
}

void CreateActorButton::focusClick(){

    Button::focusClick();
    newActor->bPickable=true;
    if (input->mouse3D==Vector3f(1e+06,1e+06,1e+06))
      newActor->setLocation(Vector3f(0,0,0));
    newActor=NULL;
}

void CreateActorButton::update(double deltaTime){

if (newActor)
    {
     int mouseX=input->mouseX;
     int mouseY=input->mouseY;

     renderer->pick(mouseX,mouseY);

     Button* myButton=dynamic_cast<Button*>(newActor);
     if (myButton)
        newActor->setLocation(Vector3f(input->mouseX,input->mouseY, 0.0f));

     else if (input->worldTarget && input->worldTarget->bPickable)
         newActor->setLocation(input->mouse3D);
    }
}

void CreateActorButton::create(){
renderer->addButton(this);
std::cout << "creating a createActorButton!";
}
